package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.common.constant.ResponseCode;
import com.lu.statistics.common.utils.Digests;
import com.lu.statistics.entity.SysAppRole;
import com.lu.statistics.entity.SysAppUser;
import com.lu.statistics.entity.SysAppUserRole;
import com.lu.statistics.exception.AccountDisabledException;
import com.lu.statistics.exception.UnknownAccountException;
import com.lu.statistics.fileService.FileService;
import com.lu.statistics.loader.DataLoader;
import com.lu.statistics.mapper.SysAppRoleMapper;
import com.lu.statistics.mapper.SysAppRolePermissionMapper;
import com.lu.statistics.mapper.SysAppUserMapper;
import com.lu.statistics.mapper.SysAppUserRoleMapper;
import com.lu.statistics.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountLockedException;
import java.util.Set;

@Service
public class AppUserServiceImpl extends ServiceImpl<SysAppUserMapper, SysAppUser> implements AppUserService {
    @Autowired
    private SysAppUserMapper userMapper;
    @Autowired
    private SysAppRolePermissionMapper rolePermissionMapper;
    @Autowired
    private FileService fileService;
    @Autowired
    private SysAppRoleMapper roleMapper;
    @Autowired
    private SysAppUserRoleMapper userRoleMapper;
    private final String ROLE_CODE = "R10003";

    @Override
    public void createUser(SysAppUser user) {
        String password = Digests.shaHex(user.getPassword(), user.getUsername());
        user.setPassword(password);
        String roleCode = DataLoader.getConfigValue("ROLE_CODE");
        SysAppRole role = roleMapper.queryRoleByRoleCode(roleCode);
        user.setBusinessRole(role.getId());
        userMapper.insert(user);
        //修改用户角色信息
        SysAppUserRole sur = new SysAppUserRole();
        sur.setRoleId(role.getId());
        sur.setUserId(user.getId());
        userRoleMapper.insert(sur);
    }

    @Override
    public void updateUser(SysAppUser user) {
        userMapper.updateById(user);
    }

    @Override
    public void updatePasswordByPhone(SysAppUser user) {
        SysAppUser appUser = userMapper.getUserByPhone(user.getTelephone());
        String password = Digests.shaHex(user.getPassword(), appUser.getUsername());
        userMapper.updatePasswordByPhone(password, user.getTelephone());
    }

    @Override
    public SysAppUser queryUserByUserNameAndPassword(String userName, String password) {
        String pwd = Digests.shaHex(password, userName);
        SysAppUser user = userMapper.getUserByUserName(userName, pwd);
        return user;
    }

    @Override
    public SysAppUser queryUserByPhone(String phone) throws AccountDisabledException, UnknownAccountException {
        SysAppUser user = userMapper.getUserByPhone(phone);
        if (user == null) {
            throw new UnknownAccountException(ResponseCode.USER_NOT_EXISTS.getMessage());
        } else if (user.getStatus() != 1) {
            throw new AccountDisabledException(ResponseCode.ACCOUNT_DISABLE.getMessage() + user.getTelephone());
        } else {
            return user;
        }
    }

    @Override
    public int queryUserCountByPhone(String phone) {
        return userMapper.getUserCountByPhone(phone);
    }

    @Override
    public SysAppUser queryUserById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public SysAppUser updateAvatar(SysAppUser user) {
        SysAppUser appUser = userMapper.selectById(user.getId());
        fileService.deleteFile(appUser.getAvatarUrl());
        userMapper.updateById(user);
        return userMapper.selectById(user.getId());

    }

    @Override
    public Set<String> queryUserPermCodeListByUser(Integer userId) {
        return rolePermissionMapper.queryUserPermCodeListByUserId(userId);
    }
}
